package kr.ac.kaist.ysproject;

import java.util.Calendar;

import kr.ac.kaist.ysproject.data.DataSmartMsgInfo;
import kr.ac.kaist.ysproject.util.DbManager;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

public class SmartMessageSettingActivity extends Activity {	
	protected DataSmartMsgInfo	m_dataSmartMsgInfo = null;
	protected String m_profileId = "";

	TimePickerDialog.OnTimeSetListener timeSetListenr = 
		new TimePickerDialog.OnTimeSetListener() {
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			TextView tv_time = (TextView)findViewById(R.id.tv_time);
			tv_time.setText(String.format("%02d", hourOfDay) + ":" + String.format("%02d", minute));
		}
	};

	DatePickerDialog.OnDateSetListener dateSetListenr = 
		new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			TextView tv_date = (TextView)findViewById(R.id.tv_date);
			tv_date.setText(String.format("%04d", year) + "." + String.format("%02d", monthOfYear + 1) + "." + String.format("%02d", dayOfMonth));
		
			TextView tv_weekly = (TextView)findViewById(R.id.tv_weeklyrepeat);
			tv_weekly.setText("");
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.smart_message_setting_layout);

		Intent intent = getIntent();

		if (intent != null) {
			m_profileId = intent.getStringExtra("ProfileId");

			m_dataSmartMsgInfo = (DataSmartMsgInfo)intent.getSerializableExtra("ObjectData");

			if (m_dataSmartMsgInfo == null) {
				Log.d("alpa44", "ObjectData in intent is null");
				m_dataSmartMsgInfo = new DataSmartMsgInfo();
			} else {
				Log.d("alpa44", "ObjectData in intent is not null");
				TextView tv = (TextView)findViewById(R.id.tv_smsgname);
				tv.setText(m_dataSmartMsgInfo.getName());

				tv = (TextView)findViewById(R.id.tv_time);
				tv.setText(m_dataSmartMsgInfo.getTime());

				tv = (TextView)findViewById(R.id.tv_timedev);
				tv.setText(m_dataSmartMsgInfo.getTimeDev());

				tv = (TextView)findViewById(R.id.tv_date);
				tv.setText(m_dataSmartMsgInfo.getDate());

				tv = (TextView)findViewById(R.id.tv_weeklyrepeat);
				tv.setText(m_dataSmartMsgInfo.getWeeklyRepeat());

				tv = (TextView)findViewById(R.id.tv_sendtype);
				tv.setText(m_dataSmartMsgInfo.getSendType());

				tv = (TextView)findViewById(R.id.tv_respect);
				tv.setText(m_dataSmartMsgInfo.getRespect());

				tv = (TextView)findViewById(R.id.tv_talkingway);
				tv.setText(m_dataSmartMsgInfo.getTalkingWay());
				
				tv = (TextView)findViewById(R.id.tv_category);
				tv.setText(m_dataSmartMsgInfo.getCategory());

				tv = (TextView)findViewById(R.id.tv_manualmsg);
				tv.setText(m_dataSmartMsgInfo.getManualMsg());
			}	
		}


		// 저장 버튼 클릭
		Button btnSave = (Button)findViewById(R.id.btn_save);

		btnSave.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO : need to save contents
				TextView tv = (TextView)findViewById(R.id.tv_smsgname);
				if (tv.getText().equals("")) {
					new AlertDialog.Builder(SmartMessageSettingActivity.this)
					.setMessage("스마트문자 이름을 입력하세요.")
					.setPositiveButton("확인", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					})
					.show();
					return;
				}

				m_dataSmartMsgInfo.setName(tv.getText().toString());

				tv = (TextView)findViewById(R.id.tv_time);
				if (tv.getText().equals("")) {
					new AlertDialog.Builder(SmartMessageSettingActivity.this)
					.setMessage("시간설정을 입력하세요.")
					.setPositiveButton("확인", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					})
					.show();
					return;
				}
				
				m_dataSmartMsgInfo.setTime(tv.getText().toString());

				tv = (TextView)findViewById(R.id.tv_timedev);
				m_dataSmartMsgInfo.setTimeDev(tv.getText().toString());

				tv = (TextView)findViewById(R.id.tv_date);
				m_dataSmartMsgInfo.setDate(tv.getText().toString());
				String strDate = tv.getText().toString();

				tv = (TextView)findViewById(R.id.tv_weeklyrepeat);
				m_dataSmartMsgInfo.setWeeklyRepeat(tv.getText().toString());
				String strWeeklyRepeat = tv.getText().toString();
				
				if (strDate.equals("") && strWeeklyRepeat.equals("")) {
					new AlertDialog.Builder(SmartMessageSettingActivity.this)
					.setMessage("일자설정 or 주간반복설정을 입력하세요.")
					.setPositiveButton("확인", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					})
					.show();
					return;
				}

				tv = (TextView)findViewById(R.id.tv_sendtype);
				if (tv.getText().equals("")) {
					new AlertDialog.Builder(SmartMessageSettingActivity.this)
					.setMessage("발신방법을 입력하세요.")
					.setPositiveButton("확인", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					})
					.show();
					return;
				}
				
				m_dataSmartMsgInfo.setSendType(tv.getText().toString());

				tv = (TextView)findViewById(R.id.tv_respect);
				m_dataSmartMsgInfo.setRespect(tv.getText().toString());
				String strRespect = tv.getText().toString();

				tv = (TextView)findViewById(R.id.tv_category);
				m_dataSmartMsgInfo.setCategory(tv.getText().toString());
				String strCategory = tv.getText().toString();

				tv = (TextView)findViewById(R.id.tv_talkingway);
				m_dataSmartMsgInfo.setTalkingWay(tv.getText().toString());
				String strTalkingWay = tv.getText().toString();

				tv = (TextView)findViewById(R.id.tv_manualmsg);
				m_dataSmartMsgInfo.setManualMsg(tv.getText().toString());
				String strManualMsg = tv.getText().toString();
				
				if (strRespect.equals("") && strCategory.equals("")
						&& strTalkingWay.equals("") && strManualMsg.equals("")) {
					new AlertDialog.Builder(SmartMessageSettingActivity.this)
					.setMessage("(존칭사용, 말투설정, 상황설정) or 메시지 직접 입력을 입력하세요.")
					.setPositiveButton("확인", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					})
					.show();
					return;
				} else if (strManualMsg.equals("") && strRespect.equals("")) {
					new AlertDialog.Builder(SmartMessageSettingActivity.this)
					.setMessage("존칭사용을  입력하세요.")
					.setPositiveButton("확인", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					})
					.show();
					return;
				} else if (strManualMsg.equals("") && strTalkingWay.equals("")) {
					new AlertDialog.Builder(SmartMessageSettingActivity.this)
					.setMessage("말투설정을  입력하세요.")
					.setPositiveButton("확인", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					})
					.show();
					return;
				} else if (strManualMsg.equals("") && strCategory.equals("")) {
					new AlertDialog.Builder(SmartMessageSettingActivity.this)
					.setMessage("상황설정을  입력하세요.")
					.setPositiveButton("확인", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					})
					.show();
					return;
				}

				m_dataSmartMsgInfo.setProfId(m_profileId);

				DbManager dbm = DbManager.getInstance(SmartMessageSettingActivity.this);
				dbm.saveSmartMsgInfo(m_dataSmartMsgInfo);

				finish();
			}
		});

		// 취소 버튼 클릭
		Button btnCancel = (Button)findViewById(R.id.btn_cancel);

		btnCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO : need to save contents
				finish();
			}
		});

		// 스마트문자 이름
		LinearLayout llName = (LinearLayout)findViewById(R.id.ll_smsgname);

		llName.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder;
				AlertDialog alertDialog;

				Context mContext = SmartMessageSettingActivity.this;
				LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
				View layout = inflater.inflate(R.layout.smart_message_setting_dlg_name, (ViewGroup)findViewById(R.id.ll_smart_message_setting_dlg_name));

				final EditText et_name = (EditText)layout.findViewById(R.id.et_name);

				et_name.setText(((TextView)findViewById(R.id.tv_smsgname)).getText());

				builder = new AlertDialog.Builder(mContext);
				builder.setView(layout);
				builder.setTitle("스마트문자 이름 입력");
				builder.setPositiveButton("확인", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						TextView tv = (TextView)findViewById(R.id.tv_smsgname);
						tv.setText(et_name.getText());
						dialog.dismiss();
					}
				});

				builder.setNegativeButton("취소", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});

				alertDialog = builder.create();
				alertDialog.show();
			}
		});

		// 시간설정
		LinearLayout llTime = (LinearLayout)findViewById(R.id.ll_time);

		llTime.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				TextView tv_time = (TextView)findViewById(R.id.tv_time);

				if (tv_time.getText().equals("")) {
					Calendar calendar = Calendar.getInstance();

					new TimePickerDialog(SmartMessageSettingActivity.this, 
							timeSetListenr,
							calendar.get(Calendar.HOUR_OF_DAY),
							calendar.get(Calendar.MINUTE),
							true).show();
				} else {
					String tok[] = tv_time.getText().toString().split(":");

					new TimePickerDialog(SmartMessageSettingActivity.this, 
							timeSetListenr,
							Integer.parseInt(tok[0]),
							Integer.parseInt(tok[1]),
							true).show();
				}
			}
		});

		// 오차시간
		LinearLayout llTimeDev = (LinearLayout)findViewById(R.id.ll_timedev);

		llTimeDev.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder;
				AlertDialog alertDialog;

				Context mContext = SmartMessageSettingActivity.this;
				LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
				View layout = inflater.inflate(R.layout.smart_message_setting_dlg_timedev, (ViewGroup)findViewById(R.id.ll_smart_message_setting_dlg_timedev));

				final EditText et_timedev = (EditText)layout.findViewById(R.id.et_timedev);

				et_timedev.setText(((TextView)findViewById(R.id.tv_timedev)).getText());

				builder = new AlertDialog.Builder(mContext);
				builder.setView(layout);
				builder.setTitle("오차 시간 입력(+,- 분단위)");
				builder.setPositiveButton("확인", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						TextView tv = (TextView)findViewById(R.id.tv_timedev);
						tv.setText(et_timedev.getText());
						dialog.dismiss();
					}
				});

				builder.setNegativeButton("취소", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});

				alertDialog = builder.create();
				alertDialog.show();
			}
		});

		// 일자설정
		LinearLayout llDate = (LinearLayout)findViewById(R.id.ll_date);

		llDate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				String strWeekly = ((TextView)findViewById(R.id.tv_weeklyrepeat)).getText().toString();
				
				if (strWeekly.equals("")) {
					TextView tv_date = (TextView)findViewById(R.id.tv_date);

					if (tv_date.getText().equals("")) {
						Calendar calendar = Calendar.getInstance();

						new DatePickerDialog(SmartMessageSettingActivity.this, 
								dateSetListenr,
								calendar.get(Calendar.YEAR),
								calendar.get(Calendar.MONTH),
								calendar.get(Calendar.DAY_OF_MONTH)).show();
					} else {
						String tok[] = tv_date.getText().toString().split("\\.");

						new DatePickerDialog(SmartMessageSettingActivity.this, 
								dateSetListenr,
								Integer.parseInt(tok[0]),
								Integer.parseInt(tok[1]) - 1,
								Integer.parseInt(tok[2])).show(); 
					}

				} else {
					AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
					builder.setMessage("일자설정 입력시 주간반복설정 입력사항은 초기화됩니다.\n계속 진행하겠습니까?");

					builder.setPositiveButton("계속", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();

							TextView tv_date = (TextView)findViewById(R.id.tv_date);

							if (tv_date.getText().equals("")) {
								Calendar calendar = Calendar.getInstance();

								new DatePickerDialog(SmartMessageSettingActivity.this, 
										dateSetListenr,
										calendar.get(Calendar.YEAR),
										calendar.get(Calendar.MONTH),
										calendar.get(Calendar.DAY_OF_MONTH)).show();
							} else {
								String tok[] = tv_date.getText().toString().split("\\.");

								new DatePickerDialog(SmartMessageSettingActivity.this, 
										dateSetListenr,
										Integer.parseInt(tok[0]),
										Integer.parseInt(tok[1]) - 1,
										Integer.parseInt(tok[2])).show(); 
							}
						}
					}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}
					);

					builder.show();
				}
			}
		});

		// 주간반복
		LinearLayout llWeeklyRepeat = (LinearLayout)findViewById(R.id.ll_weeklyrepeat);

		llWeeklyRepeat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				String strDate = ((TextView)findViewById(R.id.tv_date)).getText().toString();

				if (strDate.equals("")) {

					final String items[] = {"일", "월", "화", "수", "목", "금", "토"};
					final boolean[] itemsChecked = {false, false, false, false, false, false, false};

					AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
					builder.setTitle("주간반복 입력");

					String strSendType = ((TextView)findViewById(R.id.tv_weeklyrepeat)).getText().toString();

					String tok[] = strSendType.split(" ");

					for (int i = 0; i < tok.length; i++) {
						if (tok[i].equals("일")) {
							itemsChecked[0] = true;
						} else if (tok[i].equals("월")) {
							itemsChecked[1] = true;
						} else if (tok[i].equals("화")) {
							itemsChecked[2] = true;
						} else if (tok[i].equals("수")) {
							itemsChecked[3] = true;
						} else if (tok[i].equals("목")) {
							itemsChecked[4] = true;
						} else if (tok[i].equals("금")) {
							itemsChecked[5] = true;
						} else if (tok[i].equals("토")) {
							itemsChecked[6] = true;
						}
					}

					builder.setMultiChoiceItems(items, itemsChecked, 
							new DialogInterface.OnMultiChoiceClickListener() {

						@Override 
						public void onClick(DialogInterface dialog, int which, boolean isChecked) {
							/*
        				      Toast.makeText( getApplicationContext(), 
        				                  items[which] + "  Checked-" + Boolean.toString(isChecked), 
        				                  Toast.LENGTH_SHORT).show(); */
						}
					}).setPositiveButton("확인", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {

							SparseBooleanArray  selectedContacts = 
								((AlertDialog)dialog).getListView().getCheckedItemPositions();

							String strWeek = "";
							if (selectedContacts.get(0)) {
								strWeek += "일 ";
							}

							if (selectedContacts.get(1)) {
								strWeek += "월 ";
							}

							if (selectedContacts.get(2)) {
								strWeek += "화 ";
							}

							if (selectedContacts.get(3)) {
								strWeek += "수 ";
							}

							if (selectedContacts.get(4)) {
								strWeek += "목 ";
							}

							if (selectedContacts.get(5)) {
								strWeek += "금 ";
							}

							if (selectedContacts.get(6)) {
								strWeek += "토 ";
							}

							//Toast.makeText(SmartMessageSettingActivity.this, strWeek, Toast.LENGTH_SHORT).show();

							TextView tv = (TextView)findViewById(R.id.tv_weeklyrepeat);
							tv.setText(strWeek);

							dialog.dismiss();

						}
					}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}
					);

					builder.show();
				} else {
					AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
					builder.setMessage("주간반복설정 입력시 일자설정 입력사항은 초기화됩니다.\n계속 진행하겠습니까?");

					builder.setPositiveButton("계속", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();

							final String items[] = {"일", "월", "화", "수", "목", "금", "토"};
							final boolean[] itemsChecked = {false, false, false, false, false, false, false};

							AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
							builder.setTitle("주간반복 입력");

							String strSendType = ((TextView)findViewById(R.id.tv_weeklyrepeat)).getText().toString();

							String tok[] = strSendType.split(" ");

							for (int i = 0; i < tok.length; i++) {
								if (tok[i].equals("일")) {
									itemsChecked[0] = true;
								} else if (tok[i].equals("월")) {
									itemsChecked[1] = true;
								} else if (tok[i].equals("화")) {
									itemsChecked[2] = true;
								} else if (tok[i].equals("수")) {
									itemsChecked[3] = true;
								} else if (tok[i].equals("목")) {
									itemsChecked[4] = true;
								} else if (tok[i].equals("금")) {
									itemsChecked[5] = true;
								} else if (tok[i].equals("토")) {
									itemsChecked[6] = true;
								}
							}

							builder.setMultiChoiceItems(items, itemsChecked, 
									new DialogInterface.OnMultiChoiceClickListener() {

								@Override 
								public void onClick(DialogInterface dialog, int which, boolean isChecked) {
									/*
		        				      Toast.makeText( getApplicationContext(), 
		        				                  items[which] + "  Checked-" + Boolean.toString(isChecked), 
		        				                  Toast.LENGTH_SHORT).show(); */
								}
							}).setPositiveButton("확인", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {

									SparseBooleanArray  selectedContacts = 
										((AlertDialog)dialog).getListView().getCheckedItemPositions();

									String strWeek = "";
									if (selectedContacts.get(0)) {
										strWeek += "일 ";
									}

									if (selectedContacts.get(1)) {
										strWeek += "월 ";
									}

									if (selectedContacts.get(2)) {
										strWeek += "화 ";
									}

									if (selectedContacts.get(3)) {
										strWeek += "수 ";
									}

									if (selectedContacts.get(4)) {
										strWeek += "목 ";
									}

									if (selectedContacts.get(5)) {
										strWeek += "금 ";
									}

									if (selectedContacts.get(6)) {
										strWeek += "토 ";
									}

									//Toast.makeText(SmartMessageSettingActivity.this, strWeek, Toast.LENGTH_SHORT).show();

									TextView tv = (TextView)findViewById(R.id.tv_weeklyrepeat);
									tv.setText(strWeek);
									
									((TextView)findViewById(R.id.tv_date)).setText("");

									dialog.dismiss();

								}
							}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							}
							);

							builder.show();
						}
					}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}
					);

					builder.show();
				}
			}
		});

		// 발신방법
		LinearLayout llSendType = (LinearLayout)findViewById(R.id.ll_sendtype);

		llSendType.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//final String items[] = {"즉시", "확인", "중지"};
				final String items[] = {"즉시", "중지"};
				AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
				builder.setTitle("발신방법 입력");

				String strSendType = ((TextView)findViewById(R.id.tv_sendtype)).getText().toString();

				int initPos = 0;

				if (strSendType.equals("즉시")) {
					initPos = 0;
				} else if (strSendType.equals("확인")) {
					initPos = 1;
				} else if (strSendType.equals("중지")) {
					initPos = 2;
				}

				builder.setSingleChoiceItems(items, initPos, 
						new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// 각 리스트를 선택했을때
					}
				}).setPositiveButton("확인", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

						//Toast.makeText(SmartMessageSettingActivity.this, items[which], Toast.LENGTH_SHORT).show();
						//TextView tv = (TextView)findViewById(R.id.tv_timedev);
						//tv.setText(et_timedev.getText());
						int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
						TextView tv = (TextView)findViewById(R.id.tv_sendtype);
						tv.setText(items[selectedPosition]);

						dialog.dismiss();

					}
				}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}
				);

				builder.show();
			}
		});


		// 존칭사용
		LinearLayout llRespect = (LinearLayout)findViewById(R.id.ll_respect);

		llRespect.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String strManualMsg = ((TextView)findViewById(R.id.tv_manualmsg)).getText().toString();

				if (strManualMsg.equals("")) {
					
					final String items[] = {"사용", "사용안함"};
					AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
					builder.setTitle("존칭사용 여부");

					String strSendType = ((TextView)findViewById(R.id.tv_respect)).getText().toString();

					int initPos = 0;

					if (strSendType.equals("사용")) {
						initPos = 0;
					} else if (strSendType.equals("사용안함")) {
						initPos = 1;
					}

					builder.setSingleChoiceItems(items, initPos, 
							new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							// 각 리스트를 선택했을때
						}
					}).setPositiveButton("확인", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
							TextView tv = (TextView)findViewById(R.id.tv_respect);
							tv.setText(items[selectedPosition]);

							dialog.dismiss();

						}
					}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}
					);

					builder.show();
				} else {
					AlertDialog.Builder confirmBuilder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
					confirmBuilder.setMessage("존칭사용 입력시 메시지 직접 입력 사항은 초기화됩니다.\n계속 진행하겠습니까?");

					confirmBuilder.setPositiveButton("계속", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();

							final String items[] = {"사용", "사용안함"};
							AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
							builder.setTitle("존칭사용 여부");

							String strSendType = ((TextView)findViewById(R.id.tv_respect)).getText().toString();

							int initPos = 0;

							if (strSendType.equals("사용")) {
								initPos = 0;
							} else if (strSendType.equals("사용안함")) {
								initPos = 1;
							}

							builder.setSingleChoiceItems(items, initPos, 
									new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int whichButton) {
									// 각 리스트를 선택했을때
								}
							}).setPositiveButton("확인", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
									TextView tv = (TextView)findViewById(R.id.tv_respect);
									tv.setText(items[selectedPosition]);
									
									((TextView)findViewById(R.id.tv_manualmsg)).setText("");

									dialog.dismiss();

								}
							}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							}
							);

							builder.show();
						}
					}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}
					);

					confirmBuilder.show();
				}
			}
		});

		// 말투설정
		LinearLayout llTalkingWay = (LinearLayout)findViewById(R.id.ll_talkingway);

		llTalkingWay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String strManualMsg = ((TextView)findViewById(R.id.tv_manualmsg)).getText().toString();

				if (strManualMsg.equals("")) {

					final String items[] = {"일반", "귀여움"};
					AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
					builder.setTitle("말투설정 선택");

					String strSendType = ((TextView)findViewById(R.id.tv_talkingway)).getText().toString();

					int initPos = 0;

					if (strSendType.equals("일반")) {
						initPos = 0;
					} else if (strSendType.equals("귀여움")) {
						initPos = 1;
					}

					builder.setSingleChoiceItems(items, initPos, 
							new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							// 각 리스트를 선택했을때
						}
					}).setPositiveButton("확인", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
							TextView tv = (TextView)findViewById(R.id.tv_talkingway);
							tv.setText(items[selectedPosition]);

							dialog.dismiss();

						}
					}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}
					);

					builder.show();
				} else {
					AlertDialog.Builder confirmBuilder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
					confirmBuilder.setMessage("말투설정 입력시 메시지 직접 입력 사항은 초기화됩니다.\n계속 진행하겠습니까?");

					confirmBuilder.setPositiveButton("계속", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							
							final String items[] = {"일반", "귀여움"};
							AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
							builder.setTitle("말투설정 선택");

							String strSendType = ((TextView)findViewById(R.id.tv_talkingway)).getText().toString();

							int initPos = 0;

							if (strSendType.equals("일반")) {
								initPos = 0;
							} else if (strSendType.equals("귀여움")) {
								initPos = 1;
							}

							builder.setSingleChoiceItems(items, initPos, 
									new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int whichButton) {
									// 각 리스트를 선택했을때
								}
							}).setPositiveButton("확인", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
									TextView tv = (TextView)findViewById(R.id.tv_talkingway);
									tv.setText(items[selectedPosition]);
									
									((TextView)findViewById(R.id.tv_manualmsg)).setText("");

									dialog.dismiss();

								}
							}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							}
							);

							builder.show();

						}
					}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}
					);

					confirmBuilder.show();
				}
			}
		});

		// 상황설정
		LinearLayout llCategory = (LinearLayout)findViewById(R.id.ll_category);

		llCategory.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String strManualMsg = ((TextView)findViewById(R.id.tv_manualmsg)).getText().toString();

				if (strManualMsg.equals("")) {

					final String items[] = {"모닝콜", "식사", "사랑표현", "안부", "굿나잇", "귀가"};
					AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
					builder.setTitle("상황설정 선택");

					String strSendType = ((TextView)findViewById(R.id.tv_category)).getText().toString();

					int initPos = 0;

					if (strSendType.equals("모닝콜")) {
						initPos = 0;
					} else if (strSendType.equals("식사")) {
						initPos = 1;
					} else if (strSendType.equals("사랑표현")) {
						initPos = 2;
					} else if (strSendType.equals("안부")) {
						initPos = 3;
					} else if (strSendType.equals("굿나잇")) {
						initPos = 4;
					} else if (strSendType.equals("귀가")) {
						initPos = 5;
					}

					builder.setSingleChoiceItems(items, initPos, 
							new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							// 각 리스트를 선택했을때
						}
					}).setPositiveButton("확인", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
							TextView tv = (TextView)findViewById(R.id.tv_category);
							tv.setText(items[selectedPosition]);

							dialog.dismiss();

						}
					}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}
					);

					builder.show();
				} else {
					AlertDialog.Builder confirmBuilder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
					confirmBuilder.setMessage("상황설정 입력시 메시지 직접 입력 사항은 초기화됩니다.\n계속 진행하겠습니까?");

					confirmBuilder.setPositiveButton("계속", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							
							final String items[] = {"모닝콜", "식사", "사랑표현", "안부", "굿나잇", "귀가"};
							AlertDialog.Builder builder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
							builder.setTitle("상황설정 선택");

							String strSendType = ((TextView)findViewById(R.id.tv_category)).getText().toString();

							int initPos = 0;

							if (strSendType.equals("모닝콜")) {
								initPos = 0;
							} else if (strSendType.equals("식사")) {
								initPos = 1;
							} else if (strSendType.equals("사랑표현")) {
								initPos = 2;
							} else if (strSendType.equals("안부")) {
								initPos = 3;
							} else if (strSendType.equals("굿나잇")) {
								initPos = 4;
							} else if (strSendType.equals("귀가")) {
								initPos = 5;
							}

							builder.setSingleChoiceItems(items, initPos, 
									new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int whichButton) {
									// 각 리스트를 선택했을때
								}
							}).setPositiveButton("확인", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
									TextView tv = (TextView)findViewById(R.id.tv_category);
									tv.setText(items[selectedPosition]);
									
									((TextView)findViewById(R.id.tv_manualmsg)).setText("");

									dialog.dismiss();

								}
							}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							}
							);

							builder.show();
						}
					}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}
					);

					confirmBuilder.show();
				}
			}
		});

		// 메시지 직접 입력
		LinearLayout llManualMsg = (LinearLayout)findViewById(R.id.ll_manualmsg);

		llManualMsg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String strCategory = ((TextView)findViewById(R.id.tv_category)).getText().toString();
				String strRespect = ((TextView)findViewById(R.id.tv_respect)).getText().toString();
				String strTalkingWay = ((TextView)findViewById(R.id.tv_talkingway)).getText().toString();

				if (strCategory.equals("") && strRespect.equals("") && strTalkingWay.equals("")) {

					AlertDialog.Builder builder;
					AlertDialog alertDialog;

					Context mContext = SmartMessageSettingActivity.this;
					LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
					View layout = inflater.inflate(R.layout.smart_message_setting_dlg_manualmsg, (ViewGroup)findViewById(R.id.ll_smart_message_setting_dlg_manualmsg));

					final EditText et_msg = (EditText)layout.findViewById(R.id.et_msg);

					et_msg.setText(((TextView)findViewById(R.id.tv_manualmsg)).getText());

					builder = new AlertDialog.Builder(mContext);
					builder.setView(layout);
					builder.setTitle("메시지 직접 입력");
					builder.setPositiveButton("확인", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							TextView tv = (TextView)findViewById(R.id.tv_manualmsg);
							tv.setText(et_msg.getText());
							dialog.dismiss();
						}
					});

					builder.setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});

					alertDialog = builder.create();
					alertDialog.show();
				} else {
					AlertDialog.Builder confirmBuilder = new AlertDialog.Builder(SmartMessageSettingActivity.this);
					confirmBuilder.setMessage("메시지 직접 입력시 존칭사용/말투설정/상황설정 입력 사항은 초기화됩니다.\n계속 진행하겠습니까?");

					confirmBuilder.setPositiveButton("계속", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							
							AlertDialog.Builder builder;
							AlertDialog alertDialog;

							Context mContext = SmartMessageSettingActivity.this;
							LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
							View layout = inflater.inflate(R.layout.smart_message_setting_dlg_manualmsg, (ViewGroup)findViewById(R.id.ll_smart_message_setting_dlg_manualmsg));

							final EditText et_msg = (EditText)layout.findViewById(R.id.et_msg);

							et_msg.setText(((TextView)findViewById(R.id.tv_manualmsg)).getText());

							builder = new AlertDialog.Builder(mContext);
							builder.setView(layout);
							builder.setTitle("메시지 직접 입력");
							builder.setPositiveButton("확인", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									TextView tv = (TextView)findViewById(R.id.tv_manualmsg);
									tv.setText(et_msg.getText());
									
									((TextView)findViewById(R.id.tv_category)).setText("");
									((TextView)findViewById(R.id.tv_respect)).setText("");
									((TextView)findViewById(R.id.tv_talkingway)).setText("");
									
									dialog.dismiss();
								}
							});

							builder.setNegativeButton("취소", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							});

							alertDialog = builder.create();
							alertDialog.show();
						}
					}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}
					);

					confirmBuilder.show();
				}
			}
		});
	}

}
